//
//  ChipInstallResultViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/6.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "ChipInstallResultViewController.h"
#import "ChipInstallDetailViewController.h"
#import "ChipInstallListViewController.h"

@interface ChipInstallResultViewController ()

@end

@implementation ChipInstallResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    image.center = CGPointMake(ScreenWidth*0.5, XNavigationHeight+40.0+50.0);
    [self.view addSubview:image];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(image.frame)+30.0, ScreenWidth, 70.0)];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.0];
    [self.view addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    button.backgroundColor = COLOR(153, 153, 153, 1);
    button.frame = CGRectMake(0, 0, 100, 40.0);
    button.center = CGPointMake(ScreenWidth*0.5, CGRectGetMaxY(label.frame)+30.0);
    button.layer.cornerRadius = 3.0;
    button.layer.masksToBounds = YES;
    [self.view addSubview:button];
    
    if (_success) {
        
        self.navigationItem.title = @"安装完成";
        image.image = [UIImage imageNamed:@"suc"];
        label.text = @"安装成功";
        [button setTitle:@"继续安装" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(continInstall) forControlEvents:UIControlEventTouchUpInside];
    }else {
        
        self.navigationItem.title = @"安装失败";
        image.image = [UIImage imageNamed:@"fail"];
        label.text = [NSString stringWithFormat:@"安装失败\n\n%@", _string];
        [button setTitle:@"重新安装" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(reInstall) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)continInstall {
    
    NSArray *cons = self.navigationController.viewControllers;
    UIViewController *co = nil;
    for (UIViewController *con in cons) {
        if ([con isKindOfClass:[ChipInstallListViewController class]]) {
            co = con;
            break;
        }
    }
    if (co) {
        [self.navigationController popToViewController:co animated:YES];
    }else {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)reInstall {
    NSArray *cons = self.navigationController.viewControllers;
    UIViewController *co = nil;
    for (UIViewController *con in cons) {
        if ([con isKindOfClass:[ChipInstallDetailViewController class]]) {
            co = con;
            break;
        }
    }
    if (co) {
        [self.navigationController popToViewController:co animated:YES];
    }else {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

@end
